home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / pub / md5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-19  |  2.8 KB  |  124 lines

  1. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  2.  */
  3.  
  4. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  5. rights reserved.
  6.  
  7. RSA Data Security, Inc. makes no representations concerning either
  8. the merchantability of this software or the suitability of this
  9. software for any particular purpose. It is provided "as is"
  10. without express or implied warranty of any kind.
  11.  
  12. These notices must be retained in any copies of any part of this
  13. documentation and/or software.
  14.  */
  15.  
  16.  
  17.  
  18.   /******************************************************************
  19.  
  20.    The return value structure has been altered by Mark to fit into
  21.    cfengine instead of printing out strings on the console. Also
  22.    cf appended to the names to counteract buggy library functions in
  23.    solaris and maybe other systems which invade the name space!!!
  24.  
  25.    *******************************************************************/
  26.  
  27.  
  28. /* The following makes MD default to MD5 if it has not already been
  29.   defined with C compiler flags.
  30.  */
  31.  
  32.  
  33. #ifndef MD
  34. #define MD 5
  35. #endif
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include "global.h"
  40. #include "../src/cf.defs.h"
  41. #include "../src/cf.extern.h"
  42. #if MD == 5
  43. #include "md5.h"
  44. #endif
  45.  
  46. /* Length of test block, number of test blocks.
  47.  */
  48. #define TEST_BLOCK_LEN 1000
  49. #define TEST_BLOCK_COUNT 1000
  50.  
  51.        void MDFile PROTO_LIST ((char *));
  52. void MDFilter PROTO_LIST ((void));
  53. void MDPrint PROTO_LIST ((unsigned char [16]));
  54.  
  55. #if MD == 2
  56. #define MD_CTX MD2_CTX
  57. #define MDInit MD2Init
  58. #define MDUpdate MD2Update
  59. #define MDFinal MD2Final
  60. #endif
  61. #if MD == 4
  62. #define MD_CTX MD4_CTX
  63. #define MDInit MD4Init
  64. #define MDUpdate MD4Update
  65. #define MDFinal MD4Final
  66. #endif
  67. #if MD == 5
  68. #define MD_CTX MD5_CTX
  69. #define MDInit MD5Init
  70. #define MDUpdate MD5Update
  71. #define MDFinal MD5Final
  72. #endif
  73.  
  74. /**************************************************************/
  75.  
  76. /* Digests a file and prints the result. */
  77.  
  78. void cfMDFile (filename,digest)
  79. char *filename;
  80. unsigned char digest[16];
  81.  
  82. { FILE *file;
  83.   MD_CTX context;
  84.   int len;
  85.   unsigned char buffer[1024];
  86.  
  87. Debug2("MDFile(%s)\n",filename);
  88.   
  89. if ((file = fopen (filename, "rb")) == NULL)
  90.    {
  91.    printf ("%s can't be opened\n", filename);
  92.    }
  93. else
  94.    {
  95.    cfMD5Init (&context);
  96.    while (len = fread (buffer, 1, 1024, file))
  97.       {
  98.       cfMD5Update (&context, buffer, len);
  99.       }
  100.    
  101.    cfMD5Final (digest, &context);
  102.  
  103.    fclose (file);
  104.    }
  105. }
  106.  
  107. /**************************************************************/
  108.  
  109. /* Prints a message digest in hexadecimal */
  110.  
  111. char *cfMDPrint (digest)
  112. unsigned char digest[16];
  113.  
  114. { unsigned int i;
  115.   static char buffer[36]; 
  116.  
  117. for (i = 0; i < 16; i++)
  118.    {
  119.    sprintf((char *)(buffer+2*i),"%02x", digest[i]);
  120.    }
  121.  
  122. return buffer; 
  123. }    
  124.